package com.deceen.demo.dao;

//import com.deceen.demo.entity.DemoEntity;
import com.alibaba.fastjson.JSON;
import com.deceen.demo.entity.UserEntityEo;
//import org.apache.ibatis.annotations.Param;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author: zzx
 * @date: 2018/10/18 14:59
 * @description: 用户dao层
 */
@Component
public class UserMapper {

    @Resource
    PasswordEncoder passwordEncoder;

    private static final Map<String, UserEntityEo> user = new HashMap<>();
    static {
        }

    //通过username查询用户
    public UserEntityEo getUser(String username){
        //zhu->pass
        user.putIfAbsent("zhu", new UserEntityEo(1L, "zhu", passwordEncoder.encode("pass"), new HashSet<>()));
        //admin->admin
        user.putIfAbsent("admin", new UserEntityEo(2L,"admin",passwordEncoder.encode("admin"),new HashSet<>()));
        System.out.println("getUser user map ==> " + JSON.toJSONString(user));
        return user.get(username);

    }

    //    void register(@Param("username") String username,@Param("password") String password);
    public void register( String username, String password){
        user.put(username,new UserEntityEo(System.currentTimeMillis(),username,passwordEncoder.encode(password),new HashSet<>()));
        System.out.println("register user map ==> " + JSON.toJSONString(user));
    }

    public UserEntityEo validatePhone(String phone, String code) {
        if (phone.contains(code)) {
            return user.get("zhu");
        }
        return null;

    }
}
